<?php
/*
 * Created on Sep 7, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php
require_once APPPATH."models/model_dao.php";
class Model_productgroup extends Model_dao {

	function Model_productgroup() {
		// Call the Model constructor
		parent::Model_dao("tbl_product_gr");
	}

	/**
	 *
	 *
	 */
	function find($id) {
		$this->db->where("id", $id);
		$query = $this->db->get($this->_tbl_name);

		if ($query->num_rows() > 0) {
			return $query->first_row('array');
		}
		return null;
	}

	/**
	 *
	 *
	 */
	function fetch_all()
	{
		$this->db->order_by("sort");
		$query = $this->db->get($this->_tbl_name);

		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return null;
	}

	/**
	 *
	 *
	 */
	function insert_group($data)
	{
		$sort = $this->get_max_sort();
		$data['sort'] = $sort + 1;
		$this->db->insert($this->_tbl_name, $data);
		$id = $this->db->insert_id();
		return $id;
	}

	/**
	 *
	 *
	 */
	function update_group($data, $id)
	{
		if(intval($id)){
			$this->db->where('id', $id);
			return $this->db->update($this->_tbl_name, $data);
		}
	}

	/**
	 *
	 *
	 */
	function delete_group($id){
		if(intval($id)){
			// not detete item in group
			$this->db->where('id', $id);
			return $this->db->delete($this->_tbl_name);
		}
	}

	/**
	 *
	 *
	 */
	function get_max_sort()
	{
		$this->db->select_max('sort');
		$query = $this->db->get($this->_tbl_name);
		if ($query->num_rows() > 0) {
			$first_row = $query->first_row('array');
			return $first_row['sort'];
		}
		return 0;
	}
	
	function count_all(){
		return $this->db->count_all_results($this->_tbl_name);
	}
	
	function get_newest(){
		$this->db->select_max('id');
		$query = $this->db->get($this->_tbl_name);
		if ($query->num_rows() > 0) {
			$first_row = $query->first_row('array');
			return $first_row['id'];
		}
		return 0;
	}
}
?>